package tpi.gui.pages.caso

import dominio.ubicaciones.Lugar
import org.apache.wicket.ajax.AjaxRequestTarget
import org.apache.wicket.ajax.markup.html.form.AjaxButton
import org.apache.wicket.markup.html.WebPage
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.panel.FeedbackPanel
import org.apache.wicket.model.CompoundPropertyModel
import org.apache.wicket.model.PropertyModel
import org.uqbar.commons.model.UserException
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XAttributeModifier
import org.uqbar.wicket.xtend.XButton
import tpi.gui.model.Investigacion
import tpi.gui.pages.expedientes.VerExpedientesPage
import tpi.gui.pages.pantallafinal.PantallaFinalGanoPage
import tpi.gui.pages.pantallafinal.PantallaFinalPerdioPage
import tpi.gui.panels.caso.AeropuertoPanel
import tpi.gui.panels.caso.InterpolPanel
import tpi.gui.panels.caso.VisitarLugarPanel
import tpi.gui.panels.caso.VistaInicialPanel
import org.apache.wicket.behavior.AttributeAppender

class ResolverCasoPage extends WebPage {
	
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	private final Investigacion investigacion
	private final Form<Investigacion> form
	
	new(Investigacion investigacion) {
		this.investigacion = investigacion

		addChild(new Label("nombreCaso", new PropertyModel(this.investigacion,"caso.nombre")))
		form = new Form<Investigacion>("investigacionForm", new CompoundPropertyModel(this.investigacion))
		form.addChild(new Label("paisActual")) 
		agregarBotoneraLugares()
		form.addChild(new VistaInicialPanel("vistaActiva"))
		form.addChild(new FeedbackPanel("feedBackPanel"))
		agregarBotonExpedientes
		agregarBotonInterpol()
		agregarBotonAeropuerto()
		form.addChild(new Label("verificarOrden"))
		addChild(form)
	}
	
	
	def agregarBotonExpedientes() {
		form.addChild(new XButton("expedientes").onClick = [| verExpedientes()])
	}
	
	def agregarBotonInterpol() {
		val interpol = new AjaxButton("interpol") {
			override protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				target.add(verInterpol)
				responsePage = ResolverCasoPage.this
			}
		}
		if (this.investigacion.ordenEmitida) 
			interpol.add(new AttributeAppender("disabled",true))
		form.addChild(interpol)
	}

	def agregarBotonAeropuerto() {
		form.addChild(new AjaxButton("aeropuerto") {
			override protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				target.add(verAeropuerto)
				responsePage = ResolverCasoPage.this
			}
		})
	}
	
	def agregarBotoneraLugares() {
		form.addChild(botonVisita(1))
		form.addChild(botonVisita(2))
		form.addChild(botonVisita(3))
	}
		
	def botonVisita(Integer i) {
		val lugar = this.investigacion.paisActual.lugares.get(i-1)
		val buttonGuarida = new XButton("visitar"+i).onClick = [| irAPantallaFinal() ]
		val buttonLugar = new AjaxButton("visitar"+i) {
			override protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				target.add(visitarLugar(lugar))
			}
		}
		buttonGuarida.addChild(new Label("nombreTipoLugar", new PropertyModel(lugar,"nombreTipoLugar")))
		buttonLugar.addChild(new Label("nombreTipoLugar", new PropertyModel(lugar,"nombreTipoLugar")))
		if(lugar.esGuarida) buttonGuarida
		else buttonLugar
	}
	
	def visitarLugar(Lugar lugar) {
		val panel = new VisitarLugarPanel("vistaActiva", lugar)
		form.addOrReplace(panel)
	}
	
	def irAPantallaFinal() {
		if(investigacion.durmio())
			throw new UserException("Quien te conoce papa? Hace una orden antes!")
		if(investigacion.gano())
			responsePage = new PantallaFinalGanoPage(this.investigacion)
		else responsePage = new PantallaFinalPerdioPage(this.investigacion)
	}
	
	def verExpedientes() {
		responsePage = new VerExpedientesPage(investigacion.caso.expedientes, this)
	}
	
	def verInterpol() {
		val panel = new InterpolPanel("vistaActiva", this.investigacion)
		form.addOrReplace(panel)
	}
	
	def verAeropuerto() {
		val panel = new AeropuertoPanel("vistaActiva", this.investigacion)
		form.addOrReplace(panel)
	}
	
}